/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

DataTableConfig.order = [[0, 'desc']];

requirejs(['jquery', 'util', 'fancyBox', 'datatables'], function($, util, fancyBox, dataTables) {
    $(function() {
        util.loadOrderStatNums();
        ajaxLoadOrderlist(util);
    });

    /**
     * despatch goods
     * @params window.corder1 选中orderid
     * @returns {undefined}
     */
    function despatch() {
        var orderId = window.corder1;
        var despatchExpressCode = $('#despatchExpressCode').val();
        var expressCompany = $('#expressCompany').val();
        if (despatchExpressCode === "") {
            $('#despatchExpressCode').addClass('shake').css('border-color', '#900');
            setTimeout(function() {
                $('#despatchExpressCode').removeClass('shake');
            }, 500);
        } else {
            // 发货走起
            $.post('?/Order/ExpressReady', {
                'orderId': orderId,
                'ExpressCode': despatchExpressCode,
                'expressCompany': expressCompany
            }, function(res) {
                if (res === "1") {
                    Alert('发货成功');
                    $.fancybox.close();
                    $('#order-exp-' + orderId).slideUp();
                    loadStatData();
                } else {
                    Alert('failed');
                }
            });
        }
        orderId = null;
    }
});

function ajaxLoadOrderlist(util) {
    var dt;
    $('#orderlist').load('?/Wdmin/ajaxLoadOrderlist/page=0', function(r) {
        if (r === '0') {
            util.listEmptyTip();
        } else {
            dt = $('.dTable').dataTable(DataTableConfig).api();
            $(".various").fancybox({
                openEffect: 'fade',
                closeEffect: 'elastic',
                openSpeed: 'fast',
                closeSpeed: 'fast',
                overlayColor: '#fff',
                scrolling: false,
                afterLoad: function(r) {
                    window.corder1 = parseInt($(r.element[0]).attr('data-orderid'));
                }
            });
        }
    });
}

/**
 * 加载订单列表 全部订单
 * @returns {undefined}
 */
function ajaxLoadOrderlistAll() {
    wpLoading();
    $.get('?/Wdmin/ajaxLoadOrderlist/page=0&status=all', function(r) {
        Loading.finish();
        $('#card2-subtab2').html(r);
    });
}